package com.lingtu.searchtest04.controller;

import com.lingtu.searchtest04.dao.BagongshanquMapper;
import com.lingtu.searchtest04.entity.Bagongshanqu;
import com.lingtu.searchtest04.entity.SearchPoint;
import com.lingtu.searchtest04.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private BagongshanquMapper bagongshanquMapper;

    /**
     * test
     */
    @RequestMapping(value = "/get/test02",method = RequestMethod.GET)
    @ResponseBody
    public SearchPoint get02(){
        SearchPoint searchPoint = new SearchPoint();
        searchPoint.setId("123456");
        searchPoint.setLocation("32.15542 116.254658");
        searchPoint.setName("这是一个测试名字");
        return searchPoint;
    }

    /**
     * 根据输入的名称查询范围内相关的点信息
     */
    @PostMapping("/get/pointByName")
    @ResponseBody
    public List<Bagongshanqu> getByName(@RequestBody String name) throws IOException {
        List<SearchPoint> searchPointList = searchService.searchPointByName(name);
        List<String> ids = searchPointList.stream().map(i -> i.getId()).collect(Collectors.toList());
        List<Bagongshanqu> spotList = bagongshanquMapper.selectBatchIds(ids);
//        spotList.forEach(System.out :: println);
        return spotList;
    }

    /**
     * 根据输入的类型查询5km范围内相关的点信息
     */
    @PostMapping("/get/pointByLatLon")
    @ResponseBody
    public List<Bagongshanqu> getByPoint(@RequestBody SearchPoint point) throws IOException {
        List<SearchPoint> searchPointList = searchService.searchPointByLanLon(point);
        List<String> ids = searchPointList.stream().map(i -> i.getId()).collect(Collectors.toList());
        List<Bagongshanqu> spotList = bagongshanquMapper.selectBatchIds(ids);
//        spotList.forEach(System.out :: println);
        return spotList;

    }

    /**
     * 根据输入的名称查询范围内相关的点信息
     */
    @PostMapping("/get/pointByID")
    @ResponseBody
    public List<SearchPoint> getByID(@RequestBody String id) throws IOException {
        List<SearchPoint> searchPointList = searchService.searchPointByID(id);

        return searchPointList;
    }

    /**
     * 根据输入的类型查询范围内相关的点信息
     */
    @PostMapping("/get/pointByType")
    @ResponseBody
    public List<Bagongshanqu> getByType(@RequestBody String type) throws IOException {
        List<SearchPoint> searchPointList = searchService.searchPointByType(type);
        List<String> ids = searchPointList.stream().map(i -> i.getId()).collect(Collectors.toList());
        List<Bagongshanqu> spotList = bagongshanquMapper.selectBatchIds(ids);
//        spotList.forEach(System.out :: println);
        return spotList;
    }


}
